﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using Chapter3Applications.Models.PersistentModel;
using Chapter3Applications.Models.Abstract;

namespace Chapter3Applications.Models.ViewModels
{
    public class EnterShoppingListViewModel
    {
        //IShoppingListRepository model;

        [Required(ErrorMessage = "Please enter a shopping list name")]
        [CustomValidation(typeof(EnterShoppingListViewModel), "ValidateNoDuplicateShoppingListEntry")]
        public String ShoppingListName { get; set; }

        public Dictionary<String,Boolean> Products { get; set; }

        public EnterShoppingListViewModel()
        {
            Products = new Dictionary<String,Boolean>();
            //model = currentModel;
        }

        //Custom Validation of duplicate entries
        public static ValidationResult ValidateNoDuplicateShoppingListEntry(string ShoppingListName)
        {
            ShoppingListModelContainer model = new ShoppingListModelContainer();
            //IShoppingListRepository model = new IShoppingListRepository();
            
            if (model.ShoppingListSet.Where(x => x.ShoppingListName == ShoppingListName).Any())
            {
                return new ValidationResult("This shopping list name already exists. Please enter another shopping list name.");
            }

            return ValidationResult.Success;
        }
    }
}